package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class GlobalRank2 extends Screen {
	enum GameStatus {
		firstTimer, Regular
	}
	Game game;
	Graphics graphics;
	GameStatus status;
	int rank;
	int rankSize = 120;
	String test = "";
	String rankMsg="";
	float msgTime=0;

	public GlobalRank2(Game game) {
		super(game);
		this.game = game;
		graphics = game.getGraphics();
		rank = Settings.globalRank;
		if (rank == 0) {
			status = GameStatus.firstTimer;
		} else {
			rankDisplay();
			status = GameStatus.Regular;
		}
	}

	@Override
	public void update(float deltaTime) {
		test = "scs: "+Settings.cyberScore+"  r: " + rank + "  Sr: " + Settings.globalRank
				+ "  size: " + rankSize;
		
		if(Settings.globalRank!=rank){
			rankDisplay();
		}
		
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.x > 100 && event.x < 220) {
					if (event.y > 340 && event.y < 380) {// RANK
						game.submitScore();
					}
					if (event.y > 410 && event.y < 450
							&& status == GameStatus.Regular) {// LBOARD
						game.leaderboardScreen();
					}

				}
				if (event.y > 430 && event.x < 50) {// RETURN
					game.setScreen(new Records(game));
				}

			}
		}

	}

	private void rankDisplay() {
		Settings.save(game.getFileIO());
		rank = Settings.globalRank;
		if (status == GameStatus.firstTimer && rank == 0) {
			// beginner fails to get rank
			return;
		}

		int length = (int) (Math.log10(rank));
		rankSize = 150 - (length * 20);
		status = GameStatus.Regular;
		if(length==0){
			rankMsg="Congradulations! Top Ten List";
		}
		if(length==1){
			rankMsg="You're in the 'Top 100 Players' list";
		}
		if(length==2){
			rankMsg="You're in the 'Top 1000 Players' list";
		}
		msgTime=3;
		
		
		
		
	}

	@Override
	public void present(float deltaTime) {
		graphics.drawPixmap(Assets.blackbackground, 0, 0);
		graphics.drawSpecialText("GLOBAL", 160, 65, 56, 2, Color.BLUE);
		graphics.drawSpecialText("RANK", 160, 120, 56, 2, Color.BLUE);
		graphics.drawRect2(0, 445, 50, 35, Color.WHITE);
		graphics.drawText("back", 25, 470, 14, Color.WHITE);
		graphics.drawText(test, 160, 480, 10, Color.WHITE);

		if (status == GameStatus.firstTimer) {

			graphics.drawSpecialText("???", 160, 290, 140, 2, Color.GREEN);

			graphics.drawRect(100, 340, 120, 40, Color.GRAY);
			graphics.drawRect2(100, 340, 120, 40, Color.RED);
			graphics.drawRect2(101, 341, 118, 38, Color.RED);
			graphics.drawText("Get Rank", 160, 366, 16, Color.BLACK);

		}
		if (status == GameStatus.Regular) {

			graphics.drawSpecialText("" + rank, 160, 280, rankSize, 2,
					Color.GREEN);
			if(msgTime>0){
				graphics.drawText(rankMsg, 160, 320, 16, Color.WHITE);
				msgTime-=deltaTime;
			}

			graphics.drawRect(100, 340, 120, 40, Color.GRAY);
			graphics.drawRect2(100, 340, 120, 40, Color.RED);
			graphics.drawRect2(101, 341, 118, 38, Color.RED);
			graphics.drawText("Update Rank", 160, 366, 16, Color.BLACK);

			graphics.drawRect(100, 410, 120, 40, Color.GRAY);
			graphics.drawRect2(100, 410, 120, 40, Color.RED);
			graphics.drawRect2(101, 411, 118, 38, Color.RED);
			graphics.drawText("LeaderBoard", 160, 436, 16, Color.BLACK);

		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
